<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\CollectArticle;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct(Request $request)
    {
        $this->file = storage_path('framework/views/') . md5($request->fullUrl()) . '.html';
    }

    public function index(Request $request)
    {

        if (is_file($this->file) && $this->cache_check($this->file, 0.5)) {
            readfile($this->file);
        } else {
            $data = array();
//        $data['category'] = CollectArticle::distinct('category')->select('category')->get();
            $data['category'] = CollectArticle::distinct('category')->where('category', 'like', '%减肥%')->select('category')->get();
            foreach ($data['category'] as $keyCate => $objCate) {
                $data['catelist'][$objCate->category] = CollectArticle::getList($objCate->category,10);
            }

            $res = view('home.index.index', $data);
            // save last webPage content
            if (!config('app.debug')) {
                file_put_contents($this->file, $res);
            }

            return $res;
        }
    }

    public function ajaxData(Request $request){
        $data['catelist'] = CollectArticle::getList($request->category,10,$request->page);
        $res = view('home.index.ajaxData', $data);
        return $res;
    }

    public function detail(Request $request)
    {
        if (is_file($this->file) && $this->cache_check($this->file, 0.5)) {
            readfile($this->file);
        } else {
            $obj = CollectArticle::where('id', $request->id)->first();
            $data['otherArticle'] = CollectArticle::getList($obj->category,mt_rand(1,5),mt_rand(1,30));
            if(count($data['otherArticle']) === 0){
                $data['otherArticle'] = CollectArticle::getList($obj->category,3,1);
            }
             // $obj->content = preg_replace('/<h1>(.*?)<\/h1>/si', '', html_entity_decode($obj->content));
            $obj->content = preg_replace('/<(div|span) class="(art_box|tips_cont)">(.*?)<\/(div|span)>/si', '', html_entity_decode($obj->content));
            $obj->content = preg_replace('/<p class="summary">(.*?)<\/p>/si', '', $obj->content);

            $obj->content = str_replace($obj->title, '', $obj->content);
            $obj->content = str_replace($obj->descrition, '', $obj->content);
            $obj->content = str_replace('<p>　　</p>', '', $obj->content);
            $data['info'] = $obj;
            $res = view('home.index.detail-uikit', $data);
            // save last webPage content
            if (!config('app.debug')) {
                file_put_contents($this->file, $res);
            }

            return $res;
        }
    }

    /**
     * $filename  real file path
     * $delete_time cache save time xxx/minute
     * check cache update time
     */
    protected function cache_check($filename, $delete_time = 0)
    {
        if (is_file($filename)) {
            if ($delete_time) {
                $t  = time();
                $ft = filemtime($filename);
                if ($t - $ft < $delete_time * 3600) {
                    return true;
                }
            } else {
                return true;
            }
        }
        return false;
    }
}
